# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/14
# @File   : R14_IC_UnsettledCreditAndQuasiCurrOvduAmtCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请人未销户贷记卡、准贷记卡当前逾期校验
code:R14_IC_UnsettledCreditAndQuasiCurrOvduAmtCheck
tableName:
columnName:
groups:个人征信校验
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def CheckPre(tableName):
    table = getTable(table_Name=tableName)
    if table is None:
        return None
    elif table == [{}]:
        return 0
    else:
        Factor = 0
        flag = []
        for i in table:
            try:
                if i['state'] in [u'正常', u'冻结', u'止付', u'呆账'] and isfloat(i['currOverdueAmount']) and \
                float(i['currOverdueAmount']) > 0:
                    Factor += 1
            except:
                flag.append(None)
                continue
        if len(flag) == len(table):
            return None
        else:
            return Factor


def R14_IC_UnsettledCreditAndQuasiCurrOvduAmtCheck():
    Factor1 = CheckPre('ICRLoancardInfo002*Card')
    Factor2 = CheckPre('ICRStdLoancardInfo002*Std')

    if Factor1 is None and Factor2 is None:
        return u'缺失值'
    elif Factor1 is None and Factor2 is not None:
        return Factor2
    elif Factor1 is not None and Factor2 is None:
        return Factor1
    else:
        return Factor1 + Factor2

result = R14_IC_UnsettledCreditAndQuasiCurrOvduAmtCheck()